/**
 * Universidad del Valle de Guatemala
 * Algoritmos y estructura de Datos.
 * @autor Evelyn Lissete Paiz   11365
 * Sección: 20
 * 15 de julio, 2012
 * Programa que permite utilizar el radio por medio de una GUI.
 */

package hoja_dos;

import java.awt.BorderLayout;
import javax.swing.JOptionPane;


public class Calculadora_GUI extends javax.swing.JFrame {

    //------------------------------------------------------------------------
    // Constructor:
    //------------------------------------------------------------------------
    
    /**
     * Método que permite la cración de una nueva interfaz gráfica para la 
     * calculadora.
     * pre:
     * post: Se crea una nueva interfaz gráfica.
     */
    public Calculadora_GUI() {
        initComponents();
        //Se coloca el fondo de la ventana.
        Fondo fondo = new Fondo (423,542,"fondo.jpg");
        this.add(fondo,BorderLayout.CENTER);
        this.pack();
        //Se crea la calculadora.
        calculadora = new Calculadora ();
        this.info.disable();
        //Se muestran las instrucciones.
        JOptionPane.showMessageDialog(null,"Bienvenido, porfavor ingrese la operación, seguido de cada "
                + "operando u operador debe ingresar 1 espacio");
        
    }

    //------------------------------------------------------------------------
    // Métodos: 
    //------------------------------------------------------------------------
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        info = new javax.swing.JTextArea();
        uno = new javax.swing.JButton();
        dos = new javax.swing.JButton();
        tres = new javax.swing.JButton();
        cuatro = new javax.swing.JButton();
        cinco = new javax.swing.JButton();
        seis = new javax.swing.JButton();
        siete = new javax.swing.JButton();
        ocho = new javax.swing.JButton();
        nueve = new javax.swing.JButton();
        suma = new javax.swing.JButton();
        resta = new javax.swing.JButton();
        mul = new javax.swing.JButton();
        div = new javax.swing.JButton();
        igual = new javax.swing.JButton();
        espacio = new javax.swing.JButton();
        borrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        info.setColumns(20);
        info.setFont(new java.awt.Font("MS Mincho", 0, 18)); // NOI18N
        info.setRows(5);
        info.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jScrollPane1.setViewportView(info);

        uno.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        uno.setText("1");
        uno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unoActionPerformed(evt);
            }
        });

        dos.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        dos.setText("2");
        dos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dosActionPerformed(evt);
            }
        });

        tres.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        tres.setText("3");
        tres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tresActionPerformed(evt);
            }
        });

        cuatro.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        cuatro.setText("4");
        cuatro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cuatroActionPerformed(evt);
            }
        });

        cinco.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        cinco.setText("5");
        cinco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cincoActionPerformed(evt);
            }
        });

        seis.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        seis.setText("6");
        seis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seisActionPerformed(evt);
            }
        });

        siete.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        siete.setText("7");
        siete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sieteActionPerformed(evt);
            }
        });

        ocho.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        ocho.setText("8");
        ocho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ochoActionPerformed(evt);
            }
        });

        nueve.setFont(new java.awt.Font("Waiting for the Sunrise", 0, 18)); // NOI18N
        nueve.setText("9");
        nueve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nueveActionPerformed(evt);
            }
        });

        suma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/suma.jpg"))); // NOI18N
        suma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sumaActionPerformed(evt);
            }
        });

        resta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/resta.jpg"))); // NOI18N
        resta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restaActionPerformed(evt);
            }
        });

        mul.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/mul.jpg"))); // NOI18N
        mul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mulActionPerformed(evt);
            }
        });

        div.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/div copy.png"))); // NOI18N
        div.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                divActionPerformed(evt);
            }
        });

        igual.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/igual.png"))); // NOI18N
        igual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                igualActionPerformed(evt);
            }
        });

        espacio.setFont(new java.awt.Font("Waiting for the Sunrise", 1, 14)); // NOI18N
        espacio.setText("Espacio");
        espacio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                espacioActionPerformed(evt);
            }
        });

        borrar.setFont(new java.awt.Font("Waiting for the Sunrise", 1, 14)); // NOI18N
        borrar.setText("Borrar");
        borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(62, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(uno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cuatro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(siete, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dos, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tres, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(ocho, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cinco, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(seis, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(nueve, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(suma)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(resta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mul)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(div)))
                .addGap(68, 68, 68))
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(igual, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(espacio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(borrar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uno)
                    .addComponent(dos)
                    .addComponent(tres))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cuatro)
                    .addComponent(cinco)
                    .addComponent(seis))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siete)
                    .addComponent(ocho)
                    .addComponent(nueve))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(suma)
                    .addComponent(div)
                    .addComponent(resta)
                    .addComponent(mul))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(igual, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(espacio, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(borrar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Método encargado de colocar un + en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el símbolo +.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void sumaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sumaActionPerformed
        this.info.setText(this.info.getText()+"+");
    }//GEN-LAST:event_sumaActionPerformed

    /**
     * Método encargado de colocar un 1 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 1.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void unoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unoActionPerformed
        this.info.setText(this.info.getText()+"1");
    }//GEN-LAST:event_unoActionPerformed

    /**
     * Método encargado de colocar un 2 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 2.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void dosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dosActionPerformed
        this.info.setText(this.info.getText()+"2");
    }//GEN-LAST:event_dosActionPerformed

    /**
     * Método encargado de colocar un 3 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 3.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void tresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tresActionPerformed
        this.info.setText(this.info.getText()+"3");
    }//GEN-LAST:event_tresActionPerformed
    
    /**
     * Método encargado de colocar un 4 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 4.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void cuatroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cuatroActionPerformed
        this.info.setText(this.info.getText()+"4");
    }//GEN-LAST:event_cuatroActionPerformed

    /**
     * Método encargado de colocar un 5 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 5.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void cincoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cincoActionPerformed
        this.info.setText(this.info.getText()+"5");
    }//GEN-LAST:event_cincoActionPerformed

    /**
     * Método encargado de colocar un 6 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 6.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void seisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seisActionPerformed
        this.info.setText(this.info.getText()+"6");
    }//GEN-LAST:event_seisActionPerformed

    /**
     * Método encargado de colocar un 7 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 7.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void sieteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sieteActionPerformed
        this.info.setText(this.info.getText()+"7");
    }//GEN-LAST:event_sieteActionPerformed

    /**
     * Método encargado de colocar un 8 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 8.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void ochoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ochoActionPerformed
        this.info.setText(this.info.getText()+"8");
    }//GEN-LAST:event_ochoActionPerformed

    /**
     * Método encargado de colocar un 9 en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el número 9.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void nueveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nueveActionPerformed
        this.info.setText(this.info.getText()+"9");
    }//GEN-LAST:event_nueveActionPerformed

    /**
     * Método encargado de colocar un - en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el símbolo -.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void restaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaActionPerformed
        this.info.setText(this.info.getText()+"-");
    }//GEN-LAST:event_restaActionPerformed

    /**
     * Método encargado de colocar un * en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el símbolo *.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void mulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mulActionPerformed
        this.info.setText(this.info.getText()+"*");
    }//GEN-LAST:event_mulActionPerformed

    /**
     * Método encargado de colocar un / en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla el símbolo /.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void divActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_divActionPerformed
        this.info.setText(this.info.getText()+"/");
    }//GEN-LAST:event_divActionPerformed

    /**
     * Método encargado de colocar un espacio en la pantalla.
     * pre: Se debe seleccionar el botón.
     * post: Se coloca en la pantalla un espacio.
     * @param evt El evento que se da al selecionar el boton.
     */
    private void espacioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_espacioActionPerformed
        this.info.setText(this.info.getText()+" ");
    }//GEN-LAST:event_espacioActionPerformed

    /**
     * Método encargado de realizar la operación.
     * pre: Que se hayan ingresado todos datos.
     * post: Regresa un resultado.
     * @param evt 
     */
    private void igualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_igualActionPerformed
        try{
            JOptionPane.showMessageDialog(null, "El resultado es: " + calculadora.calcular(this.info.getText()+" "));
        }
        catch (Exception ex){
            JOptionPane.showMessageDialog(null, "Se ingresó una operación erronea");   
        }
        this.info.setText("");
    }//GEN-LAST:event_igualActionPerformed

    private void borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarActionPerformed
        try{
        this.info.setText(this.info.getText().substring(0,this.info.getText().length()-1));
        }
        catch (Exception e){}
    }//GEN-LAST:event_borrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Calculadora_GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Calculadora_GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Calculadora_GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Calculadora_GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Calculadora_GUI().setVisible(true);
            }
        });
    }
    
    //------------------------------------------------------------------------
    // Variables: 
    //------------------------------------------------------------------------
    
    //Se crea la variable que utilizará el radio.
    private Calculadora calculadora;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton borrar;
    private javax.swing.JButton cinco;
    private javax.swing.JButton cuatro;
    private javax.swing.JButton div;
    private javax.swing.JButton dos;
    private javax.swing.JButton espacio;
    private javax.swing.JButton igual;
    private javax.swing.JTextArea info;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton mul;
    private javax.swing.JButton nueve;
    private javax.swing.JButton ocho;
    private javax.swing.JButton resta;
    private javax.swing.JButton seis;
    private javax.swing.JButton siete;
    private javax.swing.JButton suma;
    private javax.swing.JButton tres;
    private javax.swing.JButton uno;
    // End of variables declaration//GEN-END:variables
}
